using System.Data;
using Caliburn.Micro;
using SimpleQueryTool.Library.Business;

namespace SimpleQueryTool.ViewModels.Workspace
{
	public class ExecuteQueryViewModel : PropertyChangedBase, IHandle<ExecuteQueryMessage>
    {
		private readonly QueryManager _queryManager;
		private readonly IEventAggregator _eventAggregator;

		public ExecuteQueryViewModel(QueryManager queryManager, IEventAggregator eventAggregator)
		{
			_eventAggregator = eventAggregator;
			_eventAggregator.Subscribe(this);
			_queryManager = queryManager;
		}

		public string Sql { get; set; }

		public void Handle(ExecuteQueryMessage message)
		{
			var resultMessage = new ExecuteQueryResultMessage();
			resultMessage.Result = _queryManager.ExecuteSql(message.Connection, Sql);

			_eventAggregator.Publish(resultMessage);
		}
    }

	public class ExecuteQueryResultMessage
	{
		public DataTable Result { get; set; }
	}
}